# -*- coding:utf-8 -*-
#作者：琴弦上的宇宙
#时间：2021年10月22日

import os
from math import *
from common import *
from openCv import *

class scPveFightChoose(myScene):
    def __init__(self,bValid):
        self.bValid=bValid
        self.taskPos=[0,0]
        self.surprisePosX=-1
        self.currPoxY=-1
        self.name='scPveFightChoose'
        self.path='resource/mercenary/pve/scPveFightChoose'
        self.tagPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('tag')]
        self.funcPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('func')]

    def resetName(self,newName):
        self.name=newName

    def enable(self):
        self.bValid=True

    def dis(self):
        self.bValid=False

    def isOwn(self,background):
        if self.bValid:
            tagList={}
            for tag in self.tagPng:
                bFind,x,y,w,h=bFindInBackground(background,tag,0.90)
                if bFind:tagList[tag.name]=(x,y,w,h)
            if (('tagSimple' in tagList) or ('tagHard' in tagList)) and \
               ('tagBack' in tagList) and ('tagVictory' not in tagList):
                return True
            else:
                return False
        else:
            return False

    def bValidButton(self,background,sur):
        if sur==None:
            for func in self.funcPng:
                if (func.name=='funcSurLove') or \
                    (func.name=='funcSurBoom') or \
                    (func.name=='funcSurDestroy') or \
                    (func.name=='funcSurGoto'):
                    bFind,x,y,w,h=bFindInBackground(background,func,0.80)
                    if bFind:
                        sur=[x,y,w,h,'funcSurprise']
                        break
        funcList={}
        for func in self.funcPng:
             if (func.name=='funcStartInter') or \
                (func.name=='funcStartGoto') or \
                (func.name=='funcStartShow') or \
                (func.name=='funcStartGet') or \
                (func.name=='funcStartSkip'):
                bFind,x,y,w,h=bFindInBackground(background,func,0.90)
                if bFind:
                    if MyGui.gRound!=90:
                        if sur!=None and 'funcSurprise' in sur:
                            MyGui.gRound=90
                            MyGui.gSurpriseAllCnt+=1
                    if MyGui.gRound!=90:
                        for func1 in self.funcPng:
                            if (func1.name=='funcSurprise'):
                                bFind,x1,y1,w1,h1=bFindInBackground(background,func1,0.80)
                                if bFind:
                                    posY=y1+h1/2+50
                                    if posY>self.currPoxY:
                                        MyGui.gRound=90
                                        MyGui.gSurpriseMissCnt+=1
                                    break
                    funcList[func.name]=(x,y,w,h)
                    break
        if 'funcStartInter' in funcList:
            #N轮放弃
            bNeedAbandon=False
            if MyGui.gRound==555:bNeedAbandon=True
            else:
                if MyGui.bTaskRunning:
                    if MyGui.gRound>MyGui.gTaskAbandon:bNeedAbandon=True
                else:
                    if MyGui.gRound>MyGui.gAbandon:bNeedAbandon=True
            if bNeedAbandon:
                for func in self.funcPng:
                    if (func.name=='funcTeamView'):
                        bFind,x,y,w,h=bFindInBackground(background,func,0.90)
                        if bFind:
                            self.resetName('scPveFightChoose_TeamView')
                            moveAndClick(x+w/2,y+h/2)
                        else:
                            self.resetName('scPveFightChoose_StartSkip')
                        break
                return True
            #正常对战
            pos=funcList['funcStartInter']
            self.resetName('scPveFightChoose_StartInter')
            moveAndClick(pos[0]+pos[2]/2,pos[1]+pos[3]/2,10)
            MyGui.gRunTime=time.time()
            if MyGui.gAbandon==999:
                MyGui.gHookTime=MyGui.G_HOOK_TIME
            return True
        elif 'funcStartGoto' in funcList:
            pos=funcList['funcStartGoto']
            self.resetName('scPveFightChoose_StartGoto')
            moveAndClick(pos[0]+pos[2]/2,pos[1]+pos[3]/2,5)
            return True
        elif 'funcStartShow' in funcList:
            pos=funcList['funcStartShow']
            self.resetName('scPveFightChoose_StartShow')
            moveAndClick(pos[0]+pos[2]/2,pos[1]+pos[3]/2)
            return True
        elif 'funcStartGet' in funcList:
            pos=funcList['funcStartGet']
            self.resetName('scPveFightChoose_StartGet')
            moveAndClick(pos[0]+pos[2]/2,pos[1]+pos[3]/2)
            return True
        elif 'funcStartSkip' in funcList:
            pos=funcList['funcStartSkip']
            self.resetName('scPveFightChoose_StartSkip')
            moveAndClick(pos[0]+pos[2]/2,pos[1]+pos[3]/2,3)
            return True
        else:
            return False

    def findTaskPos(self,background):
        for func in self.funcPng:
            if (func.name=='funcTask'):
                bFind,x,y,w,h=bFindInBackground(background,func,0.80)
                if bFind:
                    self.taskPos[0]=x+w/2
                    self.taskPos[1]=y+h/2-60
                    return True
        return False

    def findSurprisePos(self):
        #1-1不找神秘人
        if MyGui.gLevel=='1-1' or \
            (MyGui.gTaskLevel=='1-1' and MyGui.bTaskRunning):
            self.surprisePosX=-1
            return False
        #放弃2轮内不找神秘人
        if MyGui.gAbandon<=2:
            self.surprisePosX=-1
            return False
        cnt=6
        func=None
        for f in self.funcPng:
            if (f.name=='funcSurprise'):
                func=f
                break
        while (cnt>0):
            bg=SaveScreen()
            bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
            if bFind:
                self.surprisePosX=x+w/2
                Scroll(self.taskPos[0],self.taskPos[1]-100,False,12)
                return True
            else:
                self.surprisePosX=-1
                cnt-=1
                Scroll(self.taskPos[0],self.taskPos[1]-100,True,2)
        Scroll(self.taskPos[0],self.taskPos[1]-100,False,12)
        return False

    def proc(self,background):

        #循环
        bg=background
        while True:

            #找到营火位置
            if MyGui.gRound==0:
                self.findTaskPos(bg)

            #查看任务
            if MyGui.gViewTask==1:
                if MyGui.gRound==0:#查看是否完成任务
                    if self.taskPos[0]<10:
                        MyGui.gRound+=1
                        break
                    moveAndClick(self.taskPos[0],self.taskPos[1])
                    bg=SaveScreen()
                    for func in self.funcPng:
                        if (func.name=='funcStartGoto'):#寻找前往按钮
                            bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
                            if bFind:
                                self.resetName('scPveFightChoose_Task')
                                moveAndClick(x+w/2,y+h/2,2)
                                MyGui.gRound+=1
                                return True
                            else:
                                self.resetName('scPveFightChoose_StartSkip')
                                return True
                elif MyGui.gRound==1 or MyGui.gRound==555:#已经查看过任务
                    for func in self.funcPng:
                        if (func.name=='funcFire'):
                            bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
                            if bFind:
                                moveAndClick(self.taskPos[0],self.taskPos[1]-210)
                                bg=SaveScreen()
                                break
            else:
                if MyGui.gRound==0:
                    MyGui.gRound+=1

            #寻找神秘人
            if MyGui.gRound==1:
                if self.taskPos[0]>10:
                    self.findSurprisePos()

            #开始新关卡
            if self.bValidButton(bg,None):
                return True

            #寻找新关卡（mult匹配）
            allList=[]
            for func in self.funcPng:
                if (func.name=='funcSurprise') or \
                    (func.name=='funcSkip1') or \
                    (func.name=='funcSkip2') or \
                    (func.name=='funcSkip3') or \
                    (func.name=='funcSkip4') or \
                    (func.name=='funcHeroRed') or \
                    (func.name=='funcHeroBlue') or \
                    (func.name=='funcHeroGreen') or \
                    (func.name=='funcHeroBoss'):
                    bFind,okList=bFindMultInBackground(bg,func,0.80)
                    if bFind:
                        pri=0
                        for pos in okList:
                            if (func.name=='funcHeroRed') or \
                                (func.name=='funcHeroBlue') or \
                                (func.name=='funcHeroGreen') or \
                                (func.name=='funcHeroBoss'):
                                    pos[3]+=100
                                    pri=3
                            elif (func.name=='funcSurprise'):
                                pri=0
                            elif (func.name=='funcSkip1'):
                                pri=1
                            elif (func.name=='funcSkip2') or \
                                (func.name=='funcSkip3') or \
                                (func.name=='funcSkip4'):
                                pri=2
                            diff=0
                            if self.surprisePosX>0:
                                a=abs(self.surprisePosX-pos[0])
                                if a>100:diff=a
                            allList.append([pos[0],pos[1],pos[2],pos[3],\
                                func.name,pri,diff])
                            if func.name=='funcSurprise':
                                allList.append([pos[0],pos[1],pos[2],pos[3],\
                                    func.name,pri,diff])
 
            #获取有效
            allList.sort(key=lambda x:(x[1]))
            firstValid=allList[0]
            y=allList[0][1]
            newList=[]
            for pos in allList:
                if not (abs(pos[1]-y)<50):
                    firstValid=pos
                    break
            for pos in allList:
                if (abs(pos[1]-firstValid[1])<50):
                    newList.append(pos)
            newList.sort(key=lambda x:(x[6],x[5]))
            for pos in allList:
                if not (abs(pos[1]-firstValid[1])<50):
                    newList.append(pos)

            #点击操作
            for pos in newList:
                self.currPoxY=pos[1]+pos[3]/2
                moveAndClick(pos[0]+pos[2]/2,pos[1]+pos[3]/2,1)
                bg=SaveScreen()
                if self.bValidButton(bg,pos):
                    return True

            #重新截图判断
            bg=SaveScreen()
            if not self.isOwn(bg):
                return False
